<?php
/*************************************************************************
 * Generate HTML component 
 *************************************************************************/

function htmlGenSelectBox($id, $name, $label, $options, $valueSelected = '', $hasChilds = true, $disableParentCategory = false, $disableLabel = false, $addOptionAll = true) {
  $htmlSelectBox = (!$disableLabel) ? "<label for='$id'>$label</label>" : '';
  $htmlSelectBox .= "<select id='$id' name='$name'>";
  
  # add category all
  $strDisabled = ($disableParentCategory) ? 'disabled="disabled"' : '';
  $strSelected = ('0' == $valueSelected) ? 'selected="selected"' : '';
  
  $htmlSelectBox .= ($addOptionAll) ? "<option style='background-color:#cccccc; font-weight: bold;' value='0' $strSelected $strDisabled >Tất cả</option>" : '';

  foreach($options as $option) {
    if ($hasChilds) {
      $categoryChilds = $option['childs'];
      //if parent category contains no child then it will be selectable
      if (empty($categoryChilds)) $strDisabled = '';
      
      $strSelected = ($option['id'] == $valueSelected) ? 'selected="selected"' : '';
      $htmlSelectBox .= "<option style='background-color:#cccccc; font-weight: bold;' value='".$option['id']."' $strDisabled $strSelected >".$option['name']."</option>";
  
      foreach($categoryChilds as $categoryChild) {
        $strSelected = ($categoryChild['id'] == $valueSelected) ? 'selected="selected"' : '';
        $htmlSelectBox .= "<option style='padding-left: 0.6em;' value='".$categoryChild['id']."' $strSelected>".$categoryChild['name']."</option>";
      }
    } else {
      $strSelected = ($option['id'] == $valueSelected) ? 'selected="selected"' : '';
      $htmlSelectBox .= "<option value='".$option['id']."' $strSelected>".$option['name']."</option>";
    }
  }
  $htmlSelectBox .= "</select>";
  
  return $htmlSelectBox;
}

#-------------------------------------------------------------------------
function htmlGenCheckBox($id, $name, $label, $value, $isChecked = false) {
  $strChecked = ($isChecked) ? 'checked=checked' : '';
  
  $htmlCheckBox = "<label for='$id'>$label</label>";
  $htmlCheckBox .= "<input type='checkbox' id='$id' name='$name' value='$value' $strChecked />";
  
  return $htmlCheckBox;
}

#-------------------------------------------------------------------------
function htmlGenTextBox($id, $name, $label, $value = '', $size = '', $disableLabel = false) {
  $strSize = (!empty($size)) ? "size = '$size'" : ''; 

  $value = htmlspecialchars(stripslashes($value), ENT_QUOTES);

  $htmlTextBox = (!$disableLabel) ? "<label for='$id'>$label</label>" : '';
  $htmlTextBox .= "<input type='text' id='$id' name='$name' value='$value' $strSize />";

  return $htmlTextBox;
}

#-------------------------------------------------------------------------
function htmlGenPasswordBox($id, $name, $label, $value = '', $size = '') {
  $strSize = (!empty($strSize)) ? "size = '$size'" : ''; 
  
  $htmlPasswordBox = "<label for='$id'>$label</label>";
  $htmlPasswordBox .= "<input type='password' id='$id' name='$name' value='$value' $strSize />";
  
  return $htmlPasswordBox;
}

#-------------------------------------------------------------------------
function htmlGenTextArea($id, $name, $label, $value = '', $rows = '', $cols = '') {
  $strRows = (!empty($rows)) ? "rows = '$rows'" : '';
  $strCols = (!empty($rows)) ? "cols = '$cols'" : '';
  $value = stripslashes($value);
  
  $htmlTextArea = "<label for='$id'>$label</label>";
  $htmlTextArea .= "<textarea id='$id' name='$name' $strRows $strCols >$value</textarea>";

  return $htmlTextArea;
}

#-------------------------------------------------------------------------
function htmlGenHiddenField($id, $name, $value) {
  $htmlHiddenField .= "<input type='hidden' id='$id' name='$name' value='$value' />";
  
  return $htmlHiddenField;
}

#-------------------------------------------------------------------------
function htmlGenButton($id, $name, $value) {
  $htmlButton = "<input type='button' id='$id' name='$name' value='$value' />";
  
  return $htmlButton;
}

#-------------------------------------------------------------------------
function htmlGenBackButton($id, $name, $value) {
  $htmlButton = "<input type='button' id='$id' name='$name' value='$value' onClick='window.history.back()' />";
  
  return $htmlButton;
}

#-------------------------------------------------------------------------
function htmlGenSubmitButton($id, $name, $value) {
  $htmlSubmitButton = "<input type='submit' id='$id' name='$name' value='$value' />";
  
  return $htmlSubmitButton; 
}

#-------------------------------------------------------------------------
function htmlGenRadioBox($id, $name, $label, $value = '', $isChecked = false) {
  $strChecked = ($isChecked) ? 'checked=checked' : '';

  $htmlRadioBox = "<input type='radio' id='$id' name='$name' value='$value' $strChecked />";
  $htmlRadioBox .= "<label for='$id'>$label</label>";
  
  return $htmlRadioBox; 
}

#-------------------------------------------------------------------------
function htmlGenFileInputBox($id, $name, $label, $value = '', $size = '', $disableLabel = false) {
  $strSize = (!empty($size)) ? "size = '$size'" : ''; 

  $value = htmlspecialchars(stripslashes($value), ENT_QUOTES);

  $htmlTextBox = (!$disableLabel) ? "<label for='$id'>$label</label>" : '';
  $htmlTextBox .= "<input type='hidden' name='MAX_FILE_SIZE' value='500000' />";
  $htmlTextBox .= "<input type='file' id='$id' name='$name' value='$value' $strSize />";

  return $htmlTextBox;
}
